var models = require('../models/user.js');
var lession = require('../models/lession.js').lession;
var recommend = require('../models/recommend.js').recommend;
var forgetpwd = require('../models/forgetpwd.js').forgetpwd;
module.exports.autoroute = {
	get:{
		'/mylession':function(req,res){
			models.user.findById(req.session.user._id,function(err,user){
			    lession.find().where("_id").in(user.lession).exec(function(err,result){
			    	res.json({code:1,message:result});
			    });
			});

		},
		'/mylessionsearch':function(req,res){
			models.user.findById(req.session.user._id,function(err,user){
				condition = {};
				if(req.query.name)
					condition.lessionname = new RegExp(req.query.name);
			    lession.find(condition).where("_id").in(user.lession).exec(function(err,result){
			    	res.json({code:1,message:result});
			    });
			});

		},
		'/user/active':function(req,res){
			models.user.findByIdAndUpdate(req.query.id, {active:1}, function(err){
			  res.redirect("/admin/user");
			});
		},
		'/user/lock':function(req,res){
			models.user.findByIdAndUpdate(req.query.id, {active:0}, function(err){
			  res.redirect("/admin/user");
			});
		}
	},
	post:{
		'/recommend':function(req,res){
			var callback = function(err){
			  if(err){
			    debug(err);
			    res.send({code:-1,message:'保存失败'})
			  }else
			    res.send({code:1,message:'保存成功'})
			};

			var _comment = new recommend({
			  content : req.body.content, 
			  username : req.session.user.username
			});
			_comment.save(callback);
		},
		'/login':function(req,res){
			models.user.findOne({'username':req.body.username,'password':req.body.password},"username active",function(err,user){
			  if(user)
			  {
			  	if(user.active)
			  	{
			  	  req.session.user = user;
			  	  res.send({code:1,message:{_id:user._id,username:user.username}});
			  	}
			  	else
			  	  res.send({code:'-1',message:'账号尚未激活'})
			  }
			  else
			    res.send({code:'-2',message:'用户名密码错误'});
			});
		},
		'/register':function(req,res){
			models.user.count({'username':req.body.username},function(err,result){
			  if(err)
			    return;
			  if(result>0){
			    res.send({code:-2,message:'用户名已存在'});
			    return;
			  }

			  var user = new models.user({
			    email : req.body.email, 
			    username : req.body.username, 
			    name : req.body.name,
			    password:req.body.password,
			    phone:req.body.phone,
			    company:req.body.company,
			    active : 1,
			    lession:[]
			  });

			  user.save(function(err){
			    if(err){
			      debug(err);
			      res.send({code:-1,message:'保存失败'})
			    }else
			      res.send({code:1,message:'注册成功'});
			  });
			});
		},
		'/forgetPassword':function(req,res){
			  var _forgetpwd = new forgetpwd({
			    email : req.body.email, 
			    username : req.body.username,
			    phone:req.body.phone,
			    company:req.body.company,
			  });

			  _forgetpwd.save(function(err){
			    if(err){
			      debug(err);
			      res.send({code:-1,message:'保存失败'})
			    }else
			      res.send({code:1,message:'成功'});
			  });
		},
		'/editPassword':function(req,res){
			models.user.findByIdAndUpdate(req.session.user._id, {password:req.body.password}, function(err){
	  			res.send({code:1,message:'修改成功'})
	  		});
		}
	}
};

